package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.widget.Button;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.event.AddAddressSuccessEvent;
import com.chain.BrotherExchange.ui.adapter.ExtractAddressAdapter;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.viewmodel.ExtractCoinAddressViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.chain.BrotherExchange.widget.SwipeMenuRecyclerView;
import com.example.common.widget.state.NetworkStateLayout;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * 提币地址管理
 */
public class ExtractAddressManageActivity extends BaseActivity  {

    @Bind(R.id.recycler_view) SwipeMenuRecyclerView mRecyclerView;
    @Bind(R.id.network_state_layout) NetworkStateLayout mNetworkStateLayout;
    @Bind(R.id.add_extract_address_view) Button mAddExtractAddressView;

    private ExtractAddressAdapter mAdapter;
    private ExtractCoinAddressViewModel mViewModel;

    public static void launch(Activity activity) {
        launch(activity, -1);
    }

    public static void launch(Activity activity, int type) {
        activity.startActivityForResult(
                new Intent(activity, ExtractAddressManageActivity.class)
                        .putExtra(C.EXTRA_PAGE_TYPE, type)
                , C.REQ_COIN_ADDRESS);
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_extract_address_manage;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_extract_address_manage);

        // 视图模型
        mViewModel = ViewModelProviders.of(this).get(ExtractCoinAddressViewModel.class);

        // 地址列表
        int pageType = getIntent().getIntExtra(C.EXTRA_PAGE_TYPE, -1);
        mAdapter = new ExtractAddressAdapter(pageType);
        //mAdapter.setDeleteClickListener(this);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);

        // 请求提币地址
        initExtractCoinAddress();

        mNetworkStateLayout.setOnAnewRequestNetworkListener(this::initExtractCoinAddress);

        // 添加提币地址
        mAddExtractAddressView.setOnClickListener(v -> AddExtractAddressActivity.launch(v.getContext()));
    }

    private void initExtractCoinAddress() {
        mViewModel.addressList().observe(this, resource ->
                Resource.load(this, resource, mNetworkStateLayout, result ->
                        mAdapter.setAddressList(result)
                )
        );
    }

//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        ExtractCoinAddressList.DataBeanX.DataBean bean = mAdapter.getItem(position);
//        mViewModel.deleteAddress(bean.getId()).observe(this, resource -> {
//            Androids.makeText(resource.message);
//            if (resource.state == Resource.Status.SUCCESS) {
//                mAdapter.removeItem(position);
//            }
//        });
//    }

    @Subscribe
    public void addAddressEvent(AddAddressSuccessEvent event){
        initExtractCoinAddress();
    }
}
